/*
 * $Id: Control.java 123 2009-05-12 17:04:38Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.gui.deltagui.widget;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.concurrent.atomic.AtomicBoolean;

import deltadesk.gui.deltagui.graphics.Renderable;


/**
 * Control.
 * 
 * @author Andreas Meyer
 * @version $Revision: 123 $
 */
public class Control extends Widget implements Renderable {

	/** The parent composite of this control. */
	private Composite parent;
	
	/** The flag if the control needs to be rendered. */
	protected AtomicBoolean rendered = new AtomicBoolean(false);

	/** The flag if the control is opaque. */
	protected AtomicBoolean opaque = new AtomicBoolean(true);


	/**
	 * @return the parent
	 */
	public final Composite getParent() {
		return parent;
	}

	/**
	 * @param parent the parent to set
	 */
	public final void setParent(final Composite parent) {
		
		if(this.parent != parent) {

			this.parent = parent;
			
			tpe.execute(new Runnable() { public void run() { 
				
				if(parent != null) {
					onActivate();	
				} else {
					onDeactivate();
				}
			}});
		} // end if
	}

	/**
	 * Invalidate the control.
	 */
	protected void invalidate() {
		if(parent != null) {
			parent.invalidate();
		}
	}

	/**
	 * Mark the control as rendered.
	 * 
	 */
	protected final void setRendered() {
		this.rendered.set(true);
	}

	/**
	 * Mark the control as not rendered.
	 * 
	 */
	protected void setUnrendered() {
		this.rendered.set(false);
		if(getParent() != null && !opaque.get()) {
			getParent().rendered.set(false);
		}
	}
	
	/**
	 * Set the opaque flag.
	 * 
	 * @param opaque
	 */
	public void setOpaque(final boolean opaque) {
		this.opaque.set(opaque);
	}

	/**
	 * @see deltadesk.gui.deltagui.graphics.Renderable#render(java.awt.Graphics2D)
	 */
	public void render(final Graphics2D g2) {

		if(!rendered.get()) {

			if(opaque.get() && getBackgroundColor() != null) {

				final Rectangle r = g2.getClipBounds();
				g2.clearRect(r.x, r.y, r.width, r.height);
			} // end if

			renderControl(g2);

			if(getBorderColor() != null) {

				g2.setColor(getBorderColor());
				final Rectangle r = g2.getClipBounds();
				g2.drawRect(r.x, r.y, r.width - 1, r.height - 1);
			} // end if

			setRendered();
		} // end if (not rendered)
	}
	
	/**
	 * Render the control.
	 * 
	 * @param g2
	 */
	protected void renderControl(final Graphics2D g2) {
		// Sub classes override this to render the controls
	}

	/**
	 * Post a key event.
	 * 
	 * @param ke
	 */
	protected void postKeyEvent(final KeyEvent ke) {

		//TODO: implement
	}

	/** Event for component activate. */
	protected void onActivate() {}

	/** Event for component deactivate. */
	protected void onDeactivate() {}

}
